from PIL import Image
import os

def extract_gif_frames(gif_path, output_folder='frames'):
    """提取GIF的每一帧并保存为单独图片"""
    # 创建输出文件夹
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 打开GIF文件
    with Image.open(gif_path) as gif:
        # 遍历每一帧
        for frame_num in range(gif.n_frames):
            # 定位到当前帧
            gif.seek(frame_num)
            
            # 提取当前帧
            frame = gif.copy()
            
            # 保存帧 (PNG格式保留透明度)
            frame.save(os.path.join(output_folder, f'frame_{frame_num:03d}.png'))
    
    print(f"成功提取 {gif.n_frames} 帧到 {output_folder} 文件夹")

file = "test-data/masterGO.gif"
# 使用示例
extract_gif_frames(file)